package com.newzqxq.koalamoney.ui.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import com.newzqxq.koalamoney.R;
import com.newzqxq.koalamoney.common.BaseActivity;
import com.newzqxq.koalamoney.ui.fragment.ExperienceFragment;
import com.newzqxq.koalamoney.ui.fragment.InterestRatesFragment;
import com.newzqxq.koalamoney.ui.fragment.RedPacketFragment;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 红包  加息券  体验金
 */

public class VouchersActivity extends BaseActivity {

    private static final int RP = 1;
    private static final int IR = 2;
    private static final int EC = 3;
    @BindView(R.id.title_mid_tv)
    TextView title_mid_tv;

    @BindView(R.id.vouchers_red_packet)
    TextView vouchers_red_packet; //红包
    @BindView(R.id.vouchers_interest_rates)
    TextView vouchers_interest_rates; //加息券
    @BindView(R.id.vouchers_experience)
    TextView vouchers_experience; //体验券

    @BindView(R.id.tvLine1)
    TextView tvLine1;
    @BindView(R.id.tvLine2)
    TextView tvLine2;
    @BindView(R.id.tvLine3)
    TextView tvLine3;


    private RedPacketFragment redPacket;
    private InterestRatesFragment interestRates;
    private ExperienceFragment experience;

    @Override
    protected int getContentViewId() {
        return R.layout.ui_vouchers;
    }

    @Override
    protected void initData() {
        Intent getIntent = getIntent();
        int getNum = getIntent.getIntExtra("NUM", 0);
        loadingFragment(getNum);
    }

    //title
    private void setTitle() {
        title_mid_tv.setText(R.string.my_coupons);
    }

    //关闭当前
    @OnClick(R.id.title_relative_back)
    void ShutDown() {
        finish();
    }

    /**
     * 初始化 fragment
     */
    private void InitializationFragment() {
        redPacket = new RedPacketFragment();
        interestRates = new InterestRatesFragment();
        experience = new ExperienceFragment();

        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.vouchers_framelayout, redPacket)
                .add(R.id.vouchers_framelayout, interestRates)
                .add(R.id.vouchers_framelayout, experience)
                .hide(redPacket)
                .hide(interestRates)
                .hide(experience)
                .commitAllowingStateLoss();
    }

    /**
     * 红包  加息券  体验券 点击事件
     *
     * @param view
     */
    @OnClick({R.id.llRedPack, R.id.linear_vouchers_interest_rates, R.id.linear_vouchers_experience})
    void VouchersOnClicks(View view) {
        switch (view.getId()) {
            case R.id.llRedPack:
                //红包
                loadingFragment(RP);
                break;
            case R.id.linear_vouchers_interest_rates:
                //加息券
                loadingFragment(IR);
                break;
            case R.id.linear_vouchers_experience:
                //体验券
                loadingFragment(EC);
                break;
        }
    }


    /**
     * 根据  num  显示 对应的  fragment
     *
     * @param num
     */
    private void loadingFragment(int num) {
        switch (num) {
            case RP:
                //红包
                setItem(0);
                showFragment(redPacket);
                break;
            case IR:
                //加息券
                setItem(1);
                showFragment(interestRates);
                break;
            case EC:
                //体验券
                setItem(2);
                showFragment(experience);
                break;
        }
    }

    /**
     * 显示fragment
     *
     * @param fragment
     */
    private void showFragment(Fragment fragment) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .hide(redPacket)
                    .hide(interestRates)
                    .hide(experience)
                    .show(fragment)
                    .commitAllowingStateLoss();
    }

    /**
     * 显示点击的效果
     *
     * @param num
     */
    private void setItem(int num) {
        vouchers_red_packet.setTextColor(getResources().getColor(R.color.text_black));
        vouchers_interest_rates.setTextColor(getResources().getColor(R.color.text_black));
        vouchers_experience.setTextColor(getResources().getColor(R.color.text_black));
        tvLine1.setVisibility(View.INVISIBLE);
        tvLine2.setVisibility(View.INVISIBLE);
        tvLine3.setVisibility(View.INVISIBLE);
        switch (num) {
            case 0:
                vouchers_red_packet.setTextColor(getResources().getColor(R.color.red_EF0750));
                tvLine1.setVisibility(View.VISIBLE);
                break;
            case 1:
                vouchers_interest_rates.setTextColor(getResources().getColor(R.color.red_EF0750));
                tvLine2.setVisibility(View.VISIBLE);
                break;
            case 2:
                vouchers_experience.setTextColor(getResources().getColor(R.color.red_EF0750));
                tvLine3.setVisibility(View.VISIBLE);
                break;
        }
    }


    @Override
    protected void initView() {
        setTitle();
        InitializationFragment();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void clearMemory() {

    }
}
